/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-02-10
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-04-16
 * @Description: 
 */

// Flutter 核心库
import 'package:chat_client/widgets/global_init.dart' show GlobaManager;
import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:provider/provider.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
// 项目配置文件
import 'them.dart';
import 'config/api_config.dart';
import 'router/router.dart' show navigatorKey, RouteManager;
import 'package:chat_client/utils/notify/notify_helper.dart' show NotifyHelper;
// 服务层
import 'package:chat_client/services/login_service.dart';
import 'package:chat_client/services/login_service.dart' show LoginService;
import 'providers/providers.dart'; // 引入统一管理的 providers 文件
// 数据仓库
import 'package:chat_client/repositories/repository_manage.dart';
// 页面组件
import 'screens/mobile/login/login_screen.dart';
import 'screens/web/login_screen.dart';
import '/screens/mobile/home_screen.dart';
import '/screens/web/home_screen.dart';
import '/screens/test_screen.dart';

void main() async {
  // 确保 Flutter 的引擎已经初始化, 异步操作能在应用启动时正确执行
  WidgetsFlutterBinding.ensureInitialized();
  // api配置
  AppConfig.initialize();
  // 初始化本地数据库
  await initRepositories();
  // 初始化消息通知
  await NotifyHelper.initialize(navigatorKey);
  // 初始化全局组件（弹窗，loading）
  GlobaManager.init(navigatorKey);
  runApp(
    MultiProvider(
      providers: providers, // 使用统一管理的 providers 列表
      child: MyApp(loginService: LoginService.instance),
    ),
  );
}

class MyApp extends StatelessWidget {
  final LoginService loginService;
  const MyApp({super.key, required this.loginService});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'chat_client app',
      theme: lightTheme,
      home: _buildHomeScreen(),
      navigatorKey: navigatorKey, // 设置 navigatorKey
      routes: RouteManager.routes, // 注册路由
      localizationsDelegates: [
        GlobalMaterialLocalizations.delegate, // Material 组件库的本地化文本
        GlobalWidgetsLocalizations.delegate, // 控制文本方向（LTR/RTL）
      ],
      supportedLocales: const [
        Locale('zh', 'CN'), // 中文（简体）
        Locale('en', 'US'), // 英文（美国）
      ],
    );
  }

  Widget _buildHomeScreen() {
    return FutureBuilder<bool>(
      future: loginService.isLogin(), // 判断登录状态
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          // 显示加载进度条
          return const Scaffold(
              body: Center(child: CircularProgressIndicator()));
        }
        if (snapshot.hasError || !snapshot.hasData || !snapshot.data!) {
          // return kIsWeb ? WebLoginScreen() : MobileLoginScreen();
          return MobileLoginScreen();
        }
        // return kIsWeb ? WebHomeScreen() : MobileHomeScreen();
        return MobileHomeScreen();
      },
    );
  }
}
